#! /bin/bash
JAR_NAME=client-app
JAVA_OPT=""

#脚本所在目录
cd $(dirname $0)

DEPLOY_DIR=$(pwd)
JAR_DIR=$DEPLOY_DIR/$JAR_NAME.jar
LOG_PATH=$DEPLOY_DIR/logs/log.log

printUsage() {
  echo "选择功能：start: 启动 stop: 停止 restart: 重启"
  exit -1
}

# 启动的内容
startProject() {
  PROCESS=$(ps -ef | grep "java $JAVA_OPT -jar $JAR_DIR" | grep -v grep | grep -v PPID | awk '{print $2}')
  if [ -n "$PROCESS" ]; then
    echo "正在运行，无法重复启动"
  else
    nohup java $JAVA_OPT -jar $JAR_DIR >$LOG_PATH 2>&1 &
    echo '服务启动成功, 日志： tail -50f '$LOG_PATH
  fi
}

# 停止的内容
stopProject() {
  PROCESS=$(ps -ef | grep "jar $JAR_DIR" | grep -v grep | grep -v PPID | awk '{print $2}')
  if [ -n "$PROCESS" ]; then
    for i in $PROCESS; do
      echo "Kill the $1 process [ $i ]"
      kill -9 $i
    done
  else
    echo '没有服务在运行'
  fi
}
if [ $# -eq 0 ]; then
  printUsage
  exit -1
fi
if [ $1 = 'start' ]; then
  startProject
  exit -1
fi
if [ $1 = 'stop' ]; then
  stopProject
  exit -1
fi
if [ $1 = 'restart' ]; then
  stopProject
  startProject
  exit -1
fi
if [ $# -gt 0 ]; then
  echo "无效的参数:$@"
fi
